package com.wgts.wgts.control.dialog;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/*
 *
 *   data:2018/9/1111:31
 *
 */public class ToastDialogFragment extends AbstractCustomDialogFragment {
    @BindView(R.id.cancel_question_tv)
    TextView cancelQuestionTv;
    @BindView(R.id.order_cancel_tv)
    TextView orderCancelTv;
    @BindView(R.id.order_cancel_ly)
    LinearLayout orderCancelLy;
    @BindView(R.id.point_error_tv)
    TextView pointErrorTv;
    @BindView(R.id.point_error_ly)
    LinearLayout pointErrorLy;
    Unbinder unbinder;
    private View view;
    private static final String Sbundle = "ToastMessage";
    private ToastDate date;

    private OnClickListener onClickListener;

    @Override
    public void onStart() {
        super.onStart();
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window win = getDialog().getWindow();
        // 一定要设置Background，如果不设置，window属性设置无效
        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

        WindowManager.LayoutParams params = win.getAttributes();
        params.gravity = Gravity.CENTER;
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        win.setAttributes(params);
    }


    @Override
    public View onCreateDialogView() {
        Bundle bundle = getArguments();
        if (bundle != null){
            date = bundle.getParcelable(Sbundle);
        }
        view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_fragment_toast, null);
        unbinder = ButterKnife.bind(this, view);
        cancelQuestionTv.setText(date.getCancelQuestion());
        orderCancelTv.setText(date.getCancelText());
        pointErrorTv.setText(date.getPointText());
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.order_cancel_ly, R.id.point_error_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.order_cancel_ly:
                if(onClickListener != null){
                    onClickListener.onClickEnsuer();
                }
                dismiss();
                break;
            case R.id.point_error_ly:
                if(onClickListener != null){
                    onClickListener.onClickCancle();
                }
                dismiss();
                break;
        }
    }

    public static ToastDialogFragment setToastMessage(ToastDate date){
       ToastDialogFragment fragment = new ToastDialogFragment();
       Bundle bundle = new Bundle();
       bundle.putParcelable(Sbundle, date);
       fragment.setArguments(bundle);
       return fragment;
    }

    public interface OnClickListener{
        //右边的按钮
        void onClickCancle();
        //左边的按钮
        void onClickEnsuer();
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
}
